﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LRS.DTO;
using System.Data.SqlClient;

namespace LRS.DAL
{
    public static class dProduct
    {
        private static void LoadProductInfo(eProduct prod, SqlDataReader reader)
        {
            prod.ProductID = Int32.Parse(reader["ProductID"].ToString());
            prod.ProductName = reader["ProductName"].ToString();
            prod.ProductNumber = reader["ProductNumber"].ToString();
            prod.RAM = reader["RAM"].ToString();
            prod.CPU = reader["CPU"].ToString();
            prod.Graphics = reader["Graphics"].ToString();
            prod.HDD = reader["HDD"].ToString();
            prod.OtherInfo = reader["OtherInfo"].ToString();
            prod.Quantity = Int32.Parse(reader["Quantity"].ToString());
            prod.Screen = reader["Screen"].ToString();
            prod.RentingPrice = float.Parse(reader["RentingPrice"].ToString());
            prod.SellingPrice = float.Parse(reader["SellingPrice"].ToString());
            prod.ManufacturerID = Int32.Parse(reader["ManufacturerID"].ToString());
            prod.SupplierID = Int32.Parse(reader["SupplierID"].ToString());
        }

        public static eProduct GetProductByID(int ProductID)
        {
            SqlDataReader reader = SqlHelper.ExecuteReader("Select * from Product where ProductID=" + ProductID.ToString());
            reader.Read();
            eProduct product = new eProduct();
            LoadProductInfo(product, reader);
            reader.Close();
            return product;
        }
        public static List<eProduct> GetProducts()
        {
            List<eProduct> listProduct = new List<eProduct>();
            SqlDataReader reader = SqlHelper.ExecuteReader("Select * from Product Order by ProductName ASC");
            while (reader.Read())
            {
                eProduct prod = new eProduct();
                LoadProductInfo(prod, reader);
                listProduct.Add(prod);
            }
            reader.Close();
            return listProduct;
        }

        public static List<eProduct> GetProductsByKeyword(string keyword)
        {
            List<SqlParameter> parameters = new List<SqlParameter>();
            parameters.Add(new SqlParameter("@keyword", keyword));
            SqlDataReader reader = SqlHelper.ExecuteReader("Select * from Product where ProductName like '%' + @keyword + '%'", parameters);
            List<eProduct> listProduct = new List<eProduct>();
            while (reader.Read())
            {
                eProduct prod = new eProduct();
                LoadProductInfo(prod, reader);
                listProduct.Add(prod);
            }
            reader.Close();
            return listProduct;
        }


        #region Insert
        public static void InsertProduct(eProduct product)
        {
            List<SqlParameter> parameters = new List<SqlParameter>();
            parameters.Add(new SqlParameter("@ProductName", product.ProductName));
            parameters.Add(new SqlParameter("@ManufacturerID", product.ManufacturerID));
            parameters.Add(new SqlParameter("@Quantity", product.Quantity));
            parameters.Add(new SqlParameter("@RAM", product.RAM));
            parameters.Add(new SqlParameter("@CPU", product.CPU));
            parameters.Add(new SqlParameter("@HDD", product.HDD));
            parameters.Add(new SqlParameter("@Graphics", product.Graphics));
            parameters.Add(new SqlParameter("@Screen", product.Screen));
            parameters.Add(new SqlParameter("@SellingPrice", product.SellingPrice));
            parameters.Add(new SqlParameter("@RentingPrice", product.RentingPrice));
            parameters.Add(new SqlParameter("@OtherInfo", product.OtherInfo));
            parameters.Add(new SqlParameter("@SupplierID", product.SupplierID));
            SqlHelper.ExecuteNonQuery("Insert into Product values(@ProductName, @ManufacturerID, @Quantity, @RAM, @CPU, @HDD, @Graphics, @Screen, @SellingPrice, @OtherInfo, @RentingPrice, @SupplierID)", parameters);
        }
        public static void UpdateProduct(eProduct product)
        {
            List<SqlParameter> parameters = new List<SqlParameter>();
            parameters.Add(new SqlParameter("@ProductName", product.ProductName));
            parameters.Add(new SqlParameter("@ManufacturerID", product.ManufacturerID));
            parameters.Add(new SqlParameter("@Quantity", product.Quantity));
            parameters.Add(new SqlParameter("@RAM", product.RAM));
            parameters.Add(new SqlParameter("@CPU", product.CPU));
            parameters.Add(new SqlParameter("@HDD", product.HDD));
            parameters.Add(new SqlParameter("@Graphics", product.Graphics));
            parameters.Add(new SqlParameter("@Screen", product.Screen));
            parameters.Add(new SqlParameter("@SellingPrice", product.SellingPrice));
            parameters.Add(new SqlParameter("@RentingPrice", product.RentingPrice));
            parameters.Add(new SqlParameter("@OtherInfo", product.OtherInfo));
            parameters.Add(new SqlParameter("@SupplierID", product.SupplierID));

            SqlHelper.ExecuteNonQuery("Update Product set ProductName=@ProductName, ManufacturerID=@ManufacturerID, Quantity=@Quantity, RAM=@RAM, CPU=@CPU, HDD=@HDD, Graphics=@Graphics, Screen=@Screen, SellingPrice=@SellingPrice, RentingPrice=@RentingPrice, OtherInfo =@OtherInfo, SupplierID=@SupplierID where ProductID=" + product.ProductID, parameters);
        }
        public static void DeleteProductByID(int ProductID)
        {
            SqlHelper.ExecuteNonQuery("Delete from Product where ProductID=" + ProductID);
        }
        #endregion
    }
}
